/**
 * 
 * autho:zxhuan 
 * email:1123046380@qq.com
 * blog (http:www.zxhuan.com)
 * @date    2016-09-20 18:23:24
 * @version $Id$
 */

(function ($, window, document, namespace, undefined) {
	namespace.index = function () {
		// banner轮播
		var swiper = new Swiper('.swiper-container', {
			pagination: '.swiper-pagination',
			paginationClickable: true,
			nextButton: '.swiper-button-next',
			prevButton: '.swiper-button-prev',
			autoplay: 2000
		});
		var swiper1 = new Swiper('.swiper-container1', {
			slidesPerView: 1.5,
			paginationClickable: true,
			spaceBetween: 10,
		});
		// 动画加载
		$('.block').smoove({ offset: '40%' });
		// 地址选择
		$("#address").on("click", function () {
			$("#address").mobileAddress({
				"ajaxURL": "json/address.json",
				storageBox: $("#address"),
				callback: function (string, addressArr) {
					//执行回调
					/*
						string:地址
						addressArr：地址对应的坐标
					*/
					$("#address").html(string.split(" ")[1])
					console.info(string, addressArr)
				}

			});
		});
		// 判断session
		$(document).ready(function () {
			var newVisitor = isNewVisitor();// 如果是新访客
			if (newVisitor === true) {
				// 动画弹出消息框
				$("#session-mask").fadeIn(function () {
					var num = 5;
					setInterval(function () {
						num--;
						if (num == 0) {
							$("#session-mask").fadeOut();
						}
						$("#session-mask #second").html(num);
					}, 1000, function () {

					})

				});
				// 标记：已经向该访客弹出过消息。30天之内不要再弹
				setCookie("gznotes-visited", "true", 5);
			}
		});
	}
	namespace.articleList = function () {
		var counter = 0;
		// 每页展示4个
		var num = 4;
		var pageStart = 0, pageEnd = 0;
		// dropload
		$('.article-list-part').dropload({
			scrollArea: window,
			loadDownFn: function (me) {
				$.ajax({
					type: 'GET',
					url: 'json/article-list.json',
					dataType: 'json',
					success: function (data) {
						console.log(data);
						var result = '';
						counter++;
						pageEnd = num * counter;
						pageStart = pageEnd - num;
						for (var i = pageStart; i < pageEnd; i++) {
							result += '<li class="article-item"><a href="' + data.lists[i].link + '" class="article-item-link"><div class="show-img">' +
								'<img src="' + data.lists[i].bigImg + '" alt=""></div><h2 class="article-title">' + data.lists[i].title + '</h2><p class="word-warp">' + data.lists[i].desc + '</p>' +
								'<div class="clearfixed mt-5 c-999"><span class="float-l">' + data.lists[i].discuss + '评论/' + data.lists[i].reading + '阅读</span><span class="float-r">By融策网</span></div></a></li>';
							if ((i + 1) >= data.lists.length) {
								// 锁定
								me.lock();
								// 无数据
								me.noData();
								break;
							}
						}
						// 为了测试，延迟1秒加载
						setTimeout(function () {
							$('.article-list').append(result);
							// 每次数据加载完，必须重置
							me.resetload();
						}, 1000);
					},
					error: function (xhr, type) {
						console.log('Ajax error!');
						// 即使加载出错，也得重置
						me.resetload();
					}
				});
			}
		});
	}
	namespace.recommendedProducts = function () {
		var counter = 0;
		// 每页展示4个
		var num = 4;
		var pageStart = 0, pageEnd = 0;
		// dropload
		$('.recommend-list-wrap').dropload({
			scrollArea: window,
			loadDownFn: function (me) {
				$.ajax({
					type: 'GET',
					url: 'json/more.json',
					dataType: 'json',
					success: function (data) {
						console.log(data);
						var result = '';
						counter++;
						pageEnd = num * counter;
						pageStart = pageEnd - num;
						for (var i = pageStart; i < pageEnd; i++) {
							result += '<li class="recomment-list-item"><img src="' + data.lists[i].bigImg + '" alt="" class="recommend-pic">' +
								'<div class="list-item-bottom clearfixed"><img src="' + data.lists[i].smallImg + '" alt="" class="float-l show-small-icon">' +
								'<p class="float-l recommend-text">' + data.lists[i].title + '</p><div class="float-r"><span class="apply-text">200人申请</span>' +
								'<a href="' + data.lists[i].link + '" class="btn btn-recomend-apply">一键申请</a></div></div></li>';
							if ((i + 1) >= data.lists.length) {
								// 锁定
								me.lock();
								// 无数据
								me.noData();
								break;
							}
						}
						// 为了测试，延迟1秒加载
						setTimeout(function () {
							$('.recommend-list').append(result);
							// 每次数据加载完，必须重置
							me.resetload();
						}, 1000);
					},
					error: function (xhr, type) {
						console.log('Ajax error!');
						// 即使加载出错，也得重置
						me.resetload();
					}
				});
			}
		});
	}
	namespace.creditPart = function () {
		var counter = 0;
		// 每页展示4个
		var num = 4;
		var pageStart = 0, pageEnd = 0;
		// dropload
		$('.credit-part-list-wrap').dropload({
			scrollArea: window,
			loadDownFn: function (me) {
				$.ajax({
					type: 'GET',
					url: 'json/credit-list.json',
					dataType: 'json',
					success: function (data) {
						console.log(data);
						var result = '';
						counter++;
						pageEnd = num * counter;
						pageStart = pageEnd - num;
						for (var i = pageStart; i < pageEnd; i++) {
							result += ' <li class="credit-item"><a href="' + data.lists[i].link + '" class="credit-item-link clearfixed">' +
								'<div class="credit-item-show" ><img src="' + data.lists[i].bigImg + '" alt=""><div class="credit-item-text">' +
								'<div class="text"> <p class="word-warp">' + data.lists[i].title + '</p></div></div> </div> <div class="credit-item-user">' +
								'<p> <span class="user-name">' + data.lists[i].user_name + '</span> ' + data.lists[i].loan_type + "  " + data.lists[i].loan_address + '</p>' +
								'<p> 解答经营：<span class="c-blue mr-5">' + data.lists[i].discuss + '</span>贷款产品：<span class="c-blue mr-5">' +
								data.lists[i].product + '</span>种<span class="ml-5 member-type ' + data.lists[i].member_type + '">' + data.lists[i].member_text + '</span>' +
								' </p> </div></a> </li>'
							if ((i + 1) >= data.lists.length) {
								// 锁定
								me.lock();
								// 无数据
								me.noData();
								break;
							}
						}
						// 为了测试，延迟1秒加载
						setTimeout(function () {
							$('.credit-part-list').append(result);
							// 每次数据加载完，必须重置
							me.resetload();
						}, 1000);
					},
					error: function (xhr, type) {
						console.log('Ajax error!');
						// 即使加载出错，也得重置
						me.resetload();
					}
				});
			}
		});
		$(".credit-top-item").on("click", function () {
			var type = $(this).data("type");
			if (type == "credit") {
				$("#credit-mask").show();
				$("#credit-mask .icon-close").on("click", function () {
					$("#credit-mask").hide();
				})
			} else {
				$("#organize-mask").show();
				$("#organize-mask .icon-close").on("click", function () {
					$("#organize-mask").hide();
				})
			}
		})
	}
})(jQuery, window, document, window.mjPrivate = window.mjPrivate || {});
function isNewVisitor() {
	// 从cookie读取“已经向访客提示过消息”的标志位
	var flg = getCookie("gznotes-visited");
	if (flg === "") {
		return true;
	} else {
		return false;
	}
}
// 写cookie
function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
	var expires = "expires=" + d.toUTCString();
	document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
}
// 读cookie
function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1);
		if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
	}
	return "";
}